package app.netant.badbook.xuanshu;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class XuanshuDetail extends NetAntBase {
	
	private XuanshuEntity entity;

	public XuanshuDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(XuanshuEntity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		
		Elements activePart = document.select(".wrap.position span a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
		
	}
	
	public void analyseTitle(){
		Elements activePart = document.select(".wrap .show .showBox .detail .detail_info .detail_right h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
		}
	}
	
	public void analyseTags(){
		Elements activePart = document.select(".wrap .show .showBox .detail .detail_info .detail_right ul li");
		for(int i=0;i<activePart.size();i++){
			String tags = activePart.get(i).text().trim().replaceAll(" ", "");
			int idx = tags.indexOf("：");
			if(idx > 0 && idx < tags.length()-1){
				String tagName = tags.substring(0,idx);
				String tagValue = tags.substring(idx+1);
				System.out.println("【Tag】:"+tagName+"="+tagValue);	
				this.entity.setTag(tagName, tagValue);
			}
		}
		
	}
	
	public void analyseMemo(){
		Elements activePart = document.select(".wrap .show .showBox .showInfo");
		if(activePart.size()>0){
			String memo = this.getString(activePart.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
		
	}
	
	public void analyseHref(){
		
		Elements activePart = document.select(".wrap .show .showBox .showDown ul li a.downButton");
		if(activePart.size()>0){
			String href = activePart.get(0).attr("href");
			System.out.println("【Href】:"+href);
			
			int index = href.lastIndexOf(".");
			String ext = "";
			if(index>0){
				ext = href.substring(index);
			}
			
			String name = this.entity.getNovelName();
			String fullName = name + ext;
			this.entity.setNovelFullName(fullName);
			
			this.entity.setDownPath(href);
			
		}
		
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseTags();
		this.analyseMemo();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		XuanshuDetail detail = new XuanshuDetail("https://www.xuanshu.com/34221.html");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseTags();
		detail.analyseMemo();
		detail.analyseHref();
		
	}
	
}
